from PyQt4.QtCore import Qt
from PyQt4.QtGui import QWidget, QLabel, QPixmap
from ..widgets.slave_frame import SlaveFrame
from ..widgets.slave_frame import SlaveMarketTitle
from ..widgets.slave_market_monitor import SlaveMarketMonitor
from ..widgets.slave_stat_panel import SlaveStatPanel
from ..widgets.slave_trait_widget import SlaveTraitWidget
from ..widgets.slave_poster import SlavePoster
from ..widgets.mixed_button import MixedButton
from . import screen_manager
from ..data.slave_market import slave_market_souk as girls
from ..data import game_data


#from PyQt4.QtCore import *
#import pdb

class SlaveMarketScreen(QWidget):
	def __init__(self, *args, **kw):
		QWidget.__init__(self, *args, **kw)
		self.setGeometry(0, 0, 1024, 768)

		title = SlaveMarketTitle(self)
		title.setGeometry(310, 20, 454, 622)

		self.frame = SlaveFrame(self)
		self.frame.setGeometry(300, 100, 454, 622)

		self.frame.set_image('resources/camel.jpg')
		panel = QLabel(self)
		panel.setGeometry(20, 20, 242, 421)
		path = "resources/slave_info_panel.png"
		panel.setPixmap(QPixmap(path))

		self.stats = SlaveStatPanel(self)
		self.stats.move(20,20)

		self.poster = SlavePoster(self)
		self.poster.move(30, 460)
		self.poster.change_cb = self.girl_change

		traits = SlaveTraitWidget(self)
		traits.move(760, 20)
		self.traits = traits
		traits.mouse_out.connect(self.show_girl_desc)
		traits.mouse_in.connect(self.show_trait_desc)

		self.monitor = SlaveMarketMonitor(self)
		self.monitor.move(760, 475)

		buy_b = MixedButton("Purchase", self)
		buy_b.setGeometry(430, 710, 210, 50)
		buy_b.clicked.connect(self.purchase)
		self.buy_b = buy_b

		done_b = MixedButton("Depart", self)
		done_b.setGeometry(770, 710, 210, 50)
		done_b.clicked.connect(self.done_cb)
		
		self.hide()

	def done_cb(self):
		screen_manager.show("main screen")

	def show_trait_desc(self):
		self.monitor.set_text(self.traits.current_desc)

	def show_girl_desc(self):
		self.monitor.set_text(self.girl.desc)


	def girl_change(self, index):
		if index == -1:
			return
		#pyqtRemoveInputHook()
		#pdb.set_trace()

		girl = girls.slots[index].girl
		path = girl.pics.random_profile()
		self.frame.set_image(path)
		self.stats.update(girl)
		self.traits.set_traits(girl)
		self.monitor.set_text(girl.desc)
		self.girl = girl
		print "girl change: current is now ", self.girl.name

	def camel(self):
		self.frame.set_image('resources/camel.jpg')
		self.stats.camel()
		self.buy_b.setDisabled(True)
		self.poster.in_stock(False)

	def update(self, index):
		if girls.count <= 0:
			self.camel()
			return
		self.buy_b.setDisabled(False)
		self.poster.in_stock(True)

		self.poster.set_names(
			[ s.girl.name for s in girls.slots if s.girl ]
		)
		self.poster.set_current(index)
		girl = girls.slots[index].girl
		self.girl = girl
		path = girl.pics.random_profile()
		self.frame.set_image(path)
		self.stats.update(girl)

	def show(self):
		self.update(0)
		QWidget.show(self)

	def keyPressEvent(self, e):
		if e.key() == Qt.Key_Up:
			self.poster.up()
		elif e.key() == Qt.Key_Down:

			self.poster.down()


	def purchase(self):
		print "purchasing"
		# check brothels for space
		data = game_data.data
		if data.num_brothels <= 0:
			print "	*** np brothel"
			return
		brothel = data.current_brothel()

		# get girl
		girl = self.girl
		print "paying for ", girl.name
		# pay for slave through ledger

		okay = data.ledger.buy(
			girl.sale_price,
			what	= girl.name,
			category= "Slave",
			brothel	= brothel.name,
			vendor	= "Souk Market",
			quarter	= "Souk",
			approved= "Player"
		)
		if not okay:
			print "	transaction declined ", girl.name
			return

		print "adding to brothel"
		# add slave to brothel
		brothel.add(girl)

		# remove slave from market
		index = girls.remove(girl)

		self.update(index)

if __name__ == "__main__":
	import sys
	from PyQt4.QtGui import QApplication
	app = QApplication(sys.argv)

	pic = QLabel()
	pic.setGeometry(0, 0, 1024, 768)
	path = "resources/wood.jpg"
	pic.setPixmap(QPixmap(path))


	screen = SlaveMarketScreen(pic)

	pic.show()
	sys.exit(app.exec_())





